package com.aos.iot.protocol.hll.parser.data;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.aos.iot.protocol.hll.constant.HllProp;
import com.aos.iot.socket.common.parser.DataParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.aos.iot.protocol.hll.constant.HllConstant.PROTOCOL_NAME;

public abstract class HllDataParser implements DataParser {

    @Override
    public String formatLog(Dict data, List<String> items) {
        List<String> result = new ArrayList<>();
        for(String item : items) {
            HllProp prop = HllProp.valueOf(item);
            Object o = data.get(prop.getValue());
            if(o instanceof Map) {
                Map<Object, Object> map = (Map<Object, Object>) o;
                map.forEach((k,v) -> result.add(StrUtil.format("{}:{};", k, v)));
            } else {
                result.add(StrUtil.format("{}:{};", prop.getExplain(), o));
            }
        }
        return StrUtil.join("\n", result);
    }

    @Override
    public String protocol() {
        return PROTOCOL_NAME;
    }
}
